package org.skydiveapp;

import java.io.File;
import java.util.List;

import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.data.Signature;
import org.skydiveapplibrary.repository.SignatureRepository;
import org.skydiveapplibrary.ui.DrawingView;
import org.skydiveapplibrary.util.FileManager;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class SignatureActivity extends SherlockActivity
{
	// repositories
	private SignatureRepository repository = new SignatureRepository(this);
	
	// data
	private boolean isDualDisplay;

	private FileManager fileManager;
	
	@Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.signature_view);

		fileManager = new FileManager(this);
		
    	isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
    	
    	// set subtitle text
    	InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(),
    					getResources().getString(R.string.signature_header_label));
    	
    	// setup autocomplete license field
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, repository.getSignatureLicenses());
        AutoCompleteTextView signatureLicenseField = (AutoCompleteTextView) findViewById(R.id.SignatureLicenseField);
        signatureLicenseField.setAdapter(adapter);
    }
	
	private void save()
	{
		// get ui components
		DrawingView drawingView = (DrawingView) findViewById(
				R.id.SignatureDrawingView);
		TextView licenseField = (TextView) findViewById(
				R.id.SignatureLicenseField);

		// get signature data
		File file = fileManager.writeByteArrayToStorageDir(drawingView.getImageData());

//		String md5 = fileManager.putStorageFileFromCache(tempFile.getName());

		String license = licenseField.getText().toString();
		
		// get log entry ids
		List<Integer> logEntryIds = getIntent().getIntegerArrayListExtra(
				Constants.EXTRA_SELECTED_LOG_ENTRY_IDS);

		// create signature
		Signature signature = new Signature();
		signature.setFileName(file.getName());
		signature.setMD5(file.getName().replace(".jpg", ""));
		signature.setLicense(license);

		// save signature
		SignatureRepository repository = new SignatureRepository(this);
		repository.addSignature(signature, logEntryIds);

		// finish
		close();
	}
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView)  findViewById(R.id.SignatureDrawingView);
		drawingView.clear();
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getSupportMenuInflater();
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_clear, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        
		if (itemId == R.id.menu_save)
		{
			save();
			return true;
		}
		else if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else if (itemId == R.id.menu_clear)
		{
			clear();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
	}
}
